"""场景列表组件"""
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QListWidget, QListWidgetItem, QLabel
from PyQt6.QtCore import pyqtSignal
from ..video_processor import SceneInfo

class SceneListItem(QListWidgetItem):
    """场景列表项"""
    def __init__(self, scene):
        super().__init__()
        self.scene = scene
        self._update_text()
        
    def _update_text(self):
        """更新显示文本"""
        start_time = self._format_time(self.scene.start_time)
        end_time = self._format_time(self.scene.end_time)
        duration = self._format_time(self.scene.end_time - self.scene.start_time)
        
        self.setText(
            f"场景 {self.scene.start_frame}-{self.scene.end_frame}\n"
            f"时间: {start_time} - {end_time} ({duration})\n"
            f"相似度: {self.scene.similarity:.2f}"
        )
        
    def _format_time(self, seconds):
        """格式化时间"""
        minutes = int(seconds // 60)
        seconds = int(seconds % 60)
        return f"{minutes:02d}:{seconds:02d}"

class SceneList(QWidget):
    """场景列表组件"""
    scene_selected = pyqtSignal(SceneInfo)
    
    def __init__(self):
        super().__init__()
        self._init_ui()
        
    def _init_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        
        # 添加标题
        title = QLabel("检测到的场景")
        title.setStyleSheet("font-weight: bold;")
        layout.addWidget(title)
        
        # 创建列表
        self.list_widget = QListWidget()
        self.list_widget.itemClicked.connect(self._on_item_clicked)
        layout.addWidget(self.list_widget)
        
    def set_scenes(self, scenes):
        """设置场景列表"""
        self.list_widget.clear()
        for scene in scenes:
            item = SceneListItem(scene)
            self.list_widget.addItem(item)
            
    def _on_item_clicked(self, item):
        """列表项点击事件"""
        self.scene_selected.emit(item.scene) 